package Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class S_updatastu extends JPanel implements ActionListener{
    String save=null;
    JTextField 学号1,学号,姓名;
    JButton 修改,查找;
    public S_updatastu(){
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch(Exception e){System.err.println("不能设置外观: "+e);}
        学号1=new JTextField(20);
        学号=new JTextField(20);
        姓名=new JTextField(20);
        修改=new JButton("修  改");
        查找=new JButton("查  找");
        Box box1=Box.createHorizontalBox();//横放 box
        Box box2=Box.createHorizontalBox();
        Box box3=Box.createHorizontalBox();
        Box box4=Box.createHorizontalBox();
        box1.add(new JLabel("学  号:",JLabel.CENTER));
        box1.add(学号);
        box2.add(new JLabel("姓  名:",JLabel.CENTER));
        box2.add(姓名);
        box3.add(修改);
        box4.add(new JLabel("学  号:",JLabel.CENTER));
        box4.add(学号1);
        box4.add(查找);
        修改.addActionListener(this);
        查找.addActionListener(this);
        Box boxH=Box.createVerticalBox();//竖放 box
        boxH.add(box1);
        boxH.add(box2);

        boxH.add(box3);
        boxH.add(Box.createVerticalGlue());
        JPanel picPanel=new JPanel();
        JPanel messPanel=new JPanel();
        messPanel.add(box4);
        picPanel.add(boxH);
        setLayout(new BorderLayout());
        JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
        add(splitV,BorderLayout.CENTER);
        validate();
    }
    public void actionPerformed(ActionEvent e){
        Object obj=e.getSource();
        Statement stmt=null;
        ResultSet rs=null,rs1=null;
        String sql=null,sql1=null,sqlSC;
        if(obj==查找){if(学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请 填写查询的学号！ " );
else{
                sql1="select * from xuesheng where xuesheng_id='"+学号1.getText()+"'";
                try{
                    Connection dbConn1=CONN();
                    stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                    rs1=stmt.executeQuery(sql1);
                    if(rs1.next()){学号.setText(rs1.getString("xuesheng_id").trim());
                        姓名.setText(rs1.getString("xingming").trim());

                    }
                    else{JOptionPane.showMessageDialog(this,"没有这个学号的学生" );}
                    stmt.close();
                    rs1.close();
                }catch(SQLException e1){
                    System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
                }
            }
        }
        else{
            if(obj==修改){if(save==null){JOptionPane.showMessageDialog(this,"还没查找需要修 改的学生" );}
else{
                    if(学号.getText().equals("")||姓名.getText().equals("")){
                        JOptionPane.showMessageDialog(this,"学生信息填满才能修改！ " );
                    }
                    //可能有问题
                    else{sql="update xuesheng set xuesheng_id='"+学号.getText()+"',xingming='"+姓名.getText()+"',"+"where xuesheng_id='"+save+"'";
                        if(save.trim().equals(学号.getText().trim())){
                            try{
                                Connection dbConn1=CONN();
                                stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                                stmt.executeUpdate(sql);
                                save=null;
                                JOptionPane.showMessageDialog(this,"修改完成" );
                                学号.setText("");
                                姓名.setText("");
                                //系别.setText("");
                                stmt.close();
                            }catch(SQLException e1){
                                System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
                            }
                        }
                        else{sql1="select * from xuesheng where xuesheng_id='"+学号.getText()+"'";
                            try{
                                Connection dbConn1=CONN();
                                stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                                rs1=stmt.executeQuery(sql1);
                                if(rs1.next()){ JOptionPane.showMessageDialog(this,"已存在此学号学生" );
                                }
                                else{
                                    sqlSC="update xuankebiao set xuesheng_id='"+学号.getText()+"' where xuesheng_id='"+save+"'";
                                    stmt.executeUpdate(sql);
                                    stmt.executeUpdate(sqlSC);
                                    save=null;
                                    JOptionPane.showMessageDialog(null,"修改完成" );
                                    学号.setText("");
                                    姓名.setText("");
                                    //系别.setText("");
                                    }
                                stmt.close();
                                rs1.close();
                            }catch(SQLException e1){
                                System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
                            }
                        }
                    }}}}
        }
//连接数据库方法
        public static Connection CONN(){
            String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加 载JDBC 驱动
            String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=master"; //连接服务器和数据库 test
            String userName = "sa"; //默认用户名
            String userPwd = "123456"; //密码
            Connection dbConn=null;
            try {
                Class.forName(driverName);
                dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
                System.out.println("Connection Successful!"); //如 果连接成功 控制 台 输出Connection Successful!
            } catch (Exception e) {
                e.printStackTrace();
            }
            return dbConn;
        }
    }